﻿#region Usings

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Unity;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.UI.View.Converter;
using ScrumTable.UI.View.Localization;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.ViewModel;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Base class for scrum item controls
    /// </summary>
    public class ScrumItemControlBase : ScrumUserControl
    {

        /// <summary>
        /// DependencyProperty for IsPlaceForAddInfo.
        /// </summary>
        public static readonly DependencyProperty IsPlaceForAddInfoProperty =
            DependencyProperty.Register("IsPlaceForAddInfo",
                                        typeof(bool),
                                        typeof(ScrumItemControlBase),
                                        new UIPropertyMetadata(false));

        /// <summary>
        /// Gets or sets the if this control has enough space for showing additionale infos
        /// </summary>
        /// <value>The items source.</value>
        public bool IsPlaceForAddInfo
        {
            get { return (bool)GetValue(IsPlaceForAddInfoProperty); }
            set { SetValue(IsPlaceForAddInfoProperty, value); }
        }

        /// <summary>
        /// Changes the default calculated height with the given value Height =  (Height * value)
        /// But at least 79 pixel height
        /// </summary>
        public double ChangeDefaultHeight
        {
            set
            {
                MinHeight = Math.Max(79, MinHeight * value);
                MaxHeight = Math.Max(79, MaxHeight * value);
                Height = Math.Max(79, Height * value);
            }
        }

        /// <summary>
        /// Changes the default calculated height with the given value Width =  (Width * value)
        /// But at least 79 pixel width
        /// </summary>
        public double ChangeDefaultWidth
        {
            set
            {
                MinWidth = Math.Max(79, MinWidth * value);
                MaxWidth = Math.Max(79, MaxWidth * value);
                Width = Math.Max(79, Width * value);
            }
        }

        /// <summary>
        /// Stretch die Item!
        /// </summary>
        public bool ChangeToStretch
        {
            set
            {
                ChangeToStretchWithMaxSize = double.PositiveInfinity;
            }
        }

        /// <summary>
        /// Stretch die Item!
        /// </summary>
        public double ChangeToStretchWithMaxSize
        {
            set
            {
                MinWidth = 0;
                MaxWidth = value;
                Width = double.NaN;
                HorizontalAlignment = HorizontalAlignment.Stretch;
                HorizontalContentAlignment = HorizontalAlignment.Stretch;
            }
        }

        public ScrumItemControlBase()
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
               SetControlSize();
            }
        }

        protected void ShowItem(DomainBase task, ScrumUserControl item, Point position, string title)
        {
            var window = (AppScreen)Application.Current.MainWindow;
            string idTitle = task.BackEndId != "-1" ? " ( #" + task.BackEndId + " )" : "";
            
            IScrumItemViewModel<DomainBase> scrumItemViewModel = ((IScrumItemViewModel<DomainBase>)DataContext);

            var colorBinding = BindingUtil.CreateBindSourcePath("HasSynchronizationErrors", scrumItemViewModel, new ConverterErrorBoolToErrorColor(), BindingMode.OneWay);
            window.GetUnityContainer().Resolve<AppScreen>().AddScatteredItem(item, PointToScreen(position), true, true, title + idTitle, false, true, colorBinding );
        }

        
        protected void SetControlSize()
        {
            
            Common.Collections.Size controlSize = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ConfigManager>().Config.ControlSize;
            Height = controlSize.Y;
            MinHeight = controlSize.Y;
            MaxHeight = controlSize.Y;

            Width = controlSize.X;
            MinWidth = controlSize.X;
            MaxWidth = controlSize.X;

            FontSize = 10;
            IsPlaceForAddInfo = Width > 150;
            SizeChanged += OnSizeChanged;
        }

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            IsPlaceForAddInfo = Height > 100;
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if(e.Property.Name== "MaxWidth")
            {
                
            }
            base.OnPropertyChanged(e);
        } 
        
    }
}
